let pageNum = 1;
let totalPage;
$(function () {
    $.ajax({
        url: "/inboundManage/queryInboundInfo",
        ty: "post",
        dataType: "json",
        success: function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='" + r.data[j].status + "'>" + changestatus + "</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage(this)\"></span></a></li>");
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })
})

function toCPurchase(a) {
    var purchaseid = $(a).parents().siblings(".purchaseid").attr("id");
    // console.log(purchaseid);
    $.ajax({
        url:"/inboundStatics/toCPurchase",
        type: "POST",
        data:{"purchaseid":purchaseid},
        success:function () {
            location.href = "/storage/stock/stockView.html";
        }
    })
}

function changeStatus(a) {
    let s = $(".selected").attr("name");
    let purchaseid = $(".selected").parents().siblings(".purchaseid").attr("id")
    let status = '';
    if (s==="1"){
        status = "0";
    }else if (s==="0"){
        status = "1";
    }
    $.ajax({
        url:"/inboundManage/updateCgStatus",
        type: "post",
        data:{"status":status,"purchaseid":purchaseid},
        success:function () {
            location.reload();
        }
    })
}

function queryByInbooundCondition(a) {
    let warehousename = $(a).siblings("li").eq(0).children().val();
    let purchaseid = $(a).siblings("li").eq(1).children().val();
    let status = $(a).siblings("li").eq(2).children().val();
    $.ajax({
        url:"/inboundManage/queryInboundInfo",
        type:"post",
        data:{"warehousename":warehousename,"purchaseid":purchaseid,"status":status},
        dataType:"json",
        success:function (r) {
            if (r!=null){
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='"+r.data[j].status+"'>"+changestatus+"</a>\n" +
                        "          </td>\n"+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage1(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage1(this)\"></span></a></li>");
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage;
                }
            }
    })

}

function nextPage() {
    if (pageNum<totalPage){
        pageNum++;
    }
    $.ajax({
        url: "/inboundManage/queryInboundInfo",
        ty: "post",
        data:{"pageNum":pageNum},
        dataType: "json",
        success: function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='" + r.data[j].status + "'>" + changestatus + "</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })

}
function prevPage() {
    if (pageNum>1){
        pageNum--;
    }
    $.ajax({
        url: "/inboundManage/queryInboundInfo",
        ty: "post",
        data:{"pageNum":pageNum},
        datatype: "json",
        success: function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='" + r.data[j].status + "'>" + changestatus + "</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })

}
function nextPage1() {
    if (pageNum<totalPage){
        pageNum++;
    }
    let warehousename = $(".warehousename").val();
    let purchaseid = $(".purchaseid").val();
    let status = $(".status option:selected").attr("value");
    $.ajax({
        url:"/inboundManage/queryInboundInfo",
        type:"post",
        data:{"pageNum":pageNum,"warehousename":warehousename,"purchaseid":purchaseid,"status":status},
        dataType:"json",
        success:function (r) {
            if (r!=null){
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='"+r.data[j].status+"'>"+changestatus+"</a>\n" +
                        "          </td>\n"+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })
}
function prevPage1() {
    if (pageNum>1){
        pageNum--;
    }
    let warehousename = $(".warehousename").val();
    let purchaseid = $(".purchaseid").val();
    let status = $(".status option:selected").attr("value");
    $.ajax({
        url:"/inboundManage/queryInboundInfo",
        type:"post",
        data:{"pageNum":pageNum,"warehousename":warehousename,"purchaseid":purchaseid,"status":status},
        dataType:"json",
        success:function (r) {
            if (r!=null){
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let changestatus = "";
                    if (r.data[j].status === "1") {
                        status = "入库";
                        changestatus = "取消入库";
                    } else if (r.data[j].status === "0") {
                        status = "未入库";
                        changestatus = "入库";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='purchaseid' id='" + r.data[j].purchaseid + "'>" + r.data[j].purchaseid + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].warehousename + "</td>\n" +
                        "          <td>" + r.data[j].inboundtime + "</td>\n" +
                        "          <td>" + r.data[j].rkname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toCPurchase(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='tipOpen(this)' class=\"cgstatus\" name='"+r.data[j].status+"'>"+changestatus+"</a>\n" +
                        "          </td>\n"+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })
}